﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.Dtos;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Models.Login;
using STF.Blog.WebApp.Areas.ForumManage.Models.SystemMenu;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    public class LoginController : Controller
    {

        private IAdminsService _ias;
        private IAdminPermissionsService _permissions;
        private ISystemMenusService _systemMenus;
        public LoginController(IAdminsService ias, IAdminPermissionsService permissions, ISystemMenusService systemMenus)
        {
            _ias = ias;
            _permissions = permissions;
            _systemMenus = systemMenus;
        }

        [HttpGet]
        public IActionResult SignIn()
        {
            return View(new SignInViewModel());
        }

        [HttpPost]
        public async Task<IActionResult> SignIn(SignInViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                var data = await _ias.LoginAsync(model.Email,model.Pwd);
                if (data != null) 
                {
                    HttpContext.Session.SetString("LoginYes",data.Id.ToString());
                    return Redirect("~/ForumManage/Admins/Index");
                }
            }
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> GetAdminsData() 
        {
            var val = HttpContext.Session.GetString("LoginYes");
            var data = await _ias.GetByIdAsync(int.Parse(val));

            return Json(new AdminsInfoViewModel 
            {
                AdminName = data.NickName,
                AdminPhoto = data.Photo
            });
        }

        public async Task<IActionResult> GetSystemMenuList() 
        {
            /*
                select * from SystemMenus where id in
                (
	                select SystemMenusId from AdminsPermissions where rolesId = 
	                (
		                select id from Roles where Title = '普通管理员'
	                )
                )
             */

            // 1. 得到相应的权限Id
            var lnId = HttpContext.Session.GetString("LoginYes"); // 得到登入账户的Session
            var data = await _ias.GetByIdAsync(int.Parse(lnId));

            //2.在Roles当中通过这个权限Id查询得到该权限下的所有系统菜单Id
            var MenuIdList = await _permissions.GetSystemMenuIdByRolesId(data.RolesId);
            //3.查询所有系统菜单
            var allMenus = await _systemMenus.GetAllUsersAsync();
            //4.在所有系统菜单中筛查到该账户拥有的系统菜单内容
            var list = new List<SystemMenuDto>();
            for (int i = 0; i < allMenus.Count; i++)
            {
                for (int j = 0; j <MenuIdList.Count; j++)
                {
                    if (allMenus[i].Id == MenuIdList[j]) 
                    {
                        list.Add(allMenus[i]);
                        break;
                    }
                }
            }

            //5.把我们筛查到的数据转换成对应的ViewModel并发送到视图层
            var LeftMenuList = list.Where(sm => sm.ParentId == 0).Select(s => new LeftMenuListVIewModel
            {
                Id = s.Id,
                Title = s.Title,
                Link = s.Link,
                Children = GetMenu(list,s.Id)
            });
            return Json(LeftMenuList);

        }

        private List<LeftMenuListVIewModel> GetMenu(List<SystemMenuDto> data,int parentId) 
        {
            var menus = (from menu in data where menu.ParentId == parentId select menu).ToList();
            return menus == null ? new List<LeftMenuListVIewModel>() : menus.Select(s => new LeftMenuListVIewModel 
            {
                Id = s.Id,
                Title = s.Title,
                Link = s.Link,
                Children = new List<LeftMenuListVIewModel>()
            }).ToList();
        }

    }
}
